package com.iwunu.gateway.msg;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.iwunu.gateway.config.MessageCofigBean;
import com.iwunu.gateway.config.MessageSplitConfigBean;
import com.iwunu.iot.common.util.DateUtil;
import com.iwunu.iot.common.util.HexUtils;

@Component
public class MessageConversionAdapter implements InitializingBean {
	@Autowired
	private MessageCofigBean messageCofigBean;
	@Autowired
	private MessageSplitConfigBean messageSplitConfigBean;
	private Integer index;
	private String sendType01;
	private String sendType06;
	private String sendType08;
	private Integer sendTypeLength;
	private Integer deviceTypeLength;
	private Integer deviceId;
	private Integer random;
	private Integer serviceType;
	private Integer timestamp;
	private Integer count;
	private Integer macLength;
	public Integer multiple;
	private Integer offsetLength;
	public static final String OTAFlag = "A7";
	public Integer pitNumLength;
	public Integer openTypeLength;
	public Integer sendDataLength;
	public static final String sendType0c="08";
	public static final Integer fm226AllLength = 8;
	public static final String restring = "6977756e7530";// iwunu0
	public Integer getBaseLength() {
		return (index + sendTypeLength + deviceTypeLength) * multiple;
	}

	public Integer getDeviceIdEnd() {
		return getBaseLength() + deviceId * multiple;
	}

	public Integer getRandomEnd() {
		return getDeviceIdEnd() + random * multiple;
	}

	public Integer getServiceTypeEnd() {
		return getRandomEnd() + serviceType * multiple;
	}

	public Integer getTimestampEnd() {
		return getServiceTypeEnd() + timestamp * multiple;
	}

	public Integer getCountEnd() {
		return getTimestampEnd() + count * multiple;
	}

	public String getDeviceId(String msg) {
		if (msg.startsWith(messageCofigBean.getStartWith()) && msg.endsWith(messageCofigBean.getEndWith())) {
			return msg.substring(getBaseLength(), getDeviceIdEnd());
		}
		return null;
	}

	public String getRandom(String msg) {
		return msg.substring(getDeviceIdEnd(), getRandomEnd());
	}

	public String getServiceType(String msg) {
		return msg.substring(getRandomEnd(), getServiceTypeEnd());
	}

	public String getTimestamp(String msg) {
		return msg.substring(getServiceTypeEnd(), getTimestampEnd());
	}

	public Integer getCount(String msg) {
		String length = msg.substring(getTimestampEnd(), getCountEnd());
		return Integer.parseInt(length,16);
	}
	//开始人脸锁返回数据(data)解析
	public String getData(String msg) {
		Integer flaglength = messageCofigBean.getStatusLength()* multiple;
		return msg.substring(getCountEnd(), getCountEnd() + getMiniCountEnd()+flaglength+pitNumLength*multiple);
	}
	//修改密码返回数据(data)解析
	public String getUpdatePwdData(String msg) {
		Integer flaglength = messageCofigBean.getStatusLength()* multiple;
		return msg.substring(getCountEnd(), getCountEnd() + getMiniCountEnd()+flaglength+pitNumLength*multiple+sendDataLength*multiple);
	}
	//获取密码(data)解析
	public Integer getUpdatePwd(String data) {
		String pwd = data.substring(data.length()-sendDataLength*multiple,data.length());
		return  Integer.parseInt(pwd,16);
	}
	//上报日志数据返回
	public String getReportData(String msg) {
		return msg.substring(getCountEnd(), getCountEnd() + getCount(msg)*multiple);
	}
	//获取小三元组类型
	public String getMiniType(String msg) {
		return msg.substring(0,messageCofigBean.getMinitypeLength() * multiple);
	}
	//获取小三元组长度
	public String getMiniCount(String msg) {
		Integer start = messageCofigBean.getMinitypeLength() * multiple;
		Integer end = messageCofigBean.getReviceMiniCont()* multiple;
		return msg.substring(start, end);
	}
	//获取小三元组长度结束位置
	public Integer getMiniCountEnd() {
		Integer start = messageCofigBean.getMinitypeLength() * multiple;
		Integer end = messageCofigBean.getReviceMiniCont()* multiple;
		return start + end;
	}
	//获取小三元组成功标识00成功非00失败
	public Integer getSuccessFlag(String msg) {
		int start =this.getMiniCountEnd();
		Integer flaglength = messageCofigBean.getStatusLength()* multiple;
		return Integer.parseInt(msg.substring(start, start+flaglength));
	}
	//获取小三元组坑位信息
	public Integer getpitNum(String msg) {
		int start =this.getMiniCountEnd();
		Integer flaglength = messageCofigBean.getStatusLength()* multiple;
		Integer pitNumlength = messageCofigBean.getPitNumLength()* multiple;
		return Integer.parseInt(msg.substring(start+flaglength, start+flaglength+pitNumlength),16);
	}

	public String getFacesSize(String msg) {
		return msg.substring(getCountEnd(), getCountEnd() + getCount(msg));
	}

	public String getFacesIndex(String msg) {
		String data = msg.substring(getCountEnd(), getCountEnd() + getCount(msg)*2);
		return data.substring(data.length()-offsetLength*multiple, data.length());
	}

	public String getMacData(String msg) {
		return msg.substring(getCountEnd(), getCountEnd() + getCount(msg));
	}

	/**
	 * 获取白名单
	 * 
	 * @param msg
	 * @return
	 */
	public List<String> getWhiteList(String msg) {
		List<String> whiteList = new ArrayList<String>();
		Integer length = getCount(msg);
		String macData = getMacData(msg);
		for (int i = 0; i < length / macLength; i++) {
			String mac = macData.substring(i * macLength, i * macLength + macLength);
			whiteList.add(mac);
		}
		return whiteList;
	}

	/**
	 ** 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendFacesInfo(String deviceId, Integer dataLength, String data, String serviceType,
			String miniType, String random, String stamp) {
		StringBuilder sendData = new StringBuilder();
		String index = messageCofigBean.getStartWith();
		String outputService = messageCofigBean.getOutputService();
		String end = messageCofigBean.getEndWith();
		sendData.append(index).append(outputService).append(sendType01).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(dataLength + 3, 4)).append(miniType)
				.append(HexUtils.toHex(dataLength,4)).append(data).append(end);
		return sendData.toString();
	}

	
	/**
	 ** 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendDataInfo(Integer pitNum, String startTime, String endTime, Integer size) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2));
		startTime = DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8 + "";
		endTime = DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8 + "";
		sendData.append(HexUtils.toHexL(startTime, 8)).append(HexUtils.toHexL(endTime, 8));
		sendData.append(HexUtils.toHex(size, 8));
		return sendData.toString();
	}
	/**
	 ** 获取卡下发数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendDataCard(String data,Integer pitNum,String miniType, String startTime, String endTime) {
		StringBuilder send = new StringBuilder();
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2)).append(StringUtils.leftPad(data,8,"0"));
		startTime = DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8 + "";
		endTime = DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8 + "";
		sendData.append(HexUtils.toHexL(startTime, 8)).append(HexUtils.toHexL(endTime, 8));
		send.append(miniType);
		send.append(HexUtils.toHexL(sendData.toString().length()/2+"",2)).append(sendData.toString());
		return send.toString();
	}
	/**
	 ** 获取卡下发数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendDataAdvert(String data,Integer pitNum,String miniType, String startTime, String endTime) {
		Integer t1 = 0;
		Integer t2 = 0;
		StringBuilder send = new StringBuilder();
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2)).append("00");
		String[] start =startTime.split(":");
		t1 = Integer.valueOf(start[0]);
		t2 = 0;
		if(start.length>1) {
		 t2 = Integer.valueOf(start[1]);
		}
		t1 =t1*60 +t2;
		startTime = HexUtils.toHexL(t1/6+"", 2);
		String[] end =endTime.split(":");
		t1 = Integer.valueOf(end[0]);
		t2 = 0;
		if(end.length>1) {
		 t2 = Integer.valueOf(end[1]);
		}
		t1 =t1*60 +t2;
		endTime =  HexUtils.toHexL(t1/6+"", 2);
		sendData.append(startTime).append(endTime);
		sendData.append(HexUtils.toHexL(data.toString().length()/2+"",2));
		sendData.append(data);
		send.append(miniType);
		send.append(HexUtils.toHexL(sendData.toString().length()/2+"",2)).append(sendData.toString());
		return send.toString();
	}
	/**
	 ** 获取密码下发数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendDataPwd(String data,Integer pitNum,String miniType, String startTime, String endTime) {
		String send = new String();
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2)).append(HexUtils.toHexL(data, 8));
		startTime = DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8 + "";
		endTime = DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8 + "";
		sendData.append(HexUtils.toHexL(startTime, 8)).append(HexUtils.toHexL(endTime, 8));
		send=miniType+HexUtils.toHexL(sendData.toString().length()/2+"",2);
		return send+sendData.toString();
	}
	/**
	 ** 获取指纹下发数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendDataFinger(Integer pitNum,String data, String startTime, String endTime) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2)).append(data);
		startTime = DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8 + "";
		endTime = DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8 + "";
		sendData.append(HexUtils.toHexL(startTime, 8)).append(HexUtils.toHexL(endTime, 8));
		return sendData.toString();
	}
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendGeneralData(String sendType,String deviceId, Integer dataLength, String data, String serviceType, String miniType,
			String random, String stamp) {
		StringBuilder sendData = new StringBuilder();
		String index = messageCofigBean.getStartWith();
		String outputService = messageCofigBean.getOutputService();
		String end = messageCofigBean.getEndWith();
		Integer minGroupLength = messageSplitConfigBean.getMinGroupLength();
		sendData.append(index).append(outputService).append(sendType).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(dataLength + minGroupLength+1, 4)).append(miniType)
				.append(HexUtils.toHex(dataLength, 2)).append(data).append(end);
		return sendData.toString();
	}
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendAddOpenTypeData01(String deviceId, Integer dataLength, String data, String serviceType,
			String random, String stamp) {
		StringBuilder sendData = new StringBuilder();
		String index = messageCofigBean.getStartWith();
		String outputService = messageCofigBean.getOutputService();
		String end = messageCofigBean.getEndWith();
		sendData.append(index).append(outputService).append(sendType01).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(dataLength , 4))
				.append(data).append(end);
		return sendData.toString();
	}
	public String getSendAddOpenTypeData(String sendType ,String deviceId, Integer dataLength, String data, String serviceType,
			String random, String stamp) {
		StringBuilder sendData = new StringBuilder();
		String index = messageCofigBean.getStartWith();
		String outputService = messageCofigBean.getOutputService();
		String end = messageCofigBean.getEndWith();
		sendData.append(index).append(outputService).append(sendType).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(dataLength , 4))
				.append(data).append(end);
		return sendData.toString();
	}
	public String getSendAddOpenTypeData08(String deviceId, Integer dataLength, String data, String serviceType,
			String random, String stamp) {
		StringBuilder sendData = new StringBuilder();
		String index = messageCofigBean.getStartWith();
		String outputService = messageCofigBean.getOutputService();
		String end = messageCofigBean.getEndWith();
		sendData.append(index).append(outputService).append(sendType08).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(dataLength , 4))
				.append(data).append(end);
		return sendData.toString();
	}
	/**
	 * 获取下发删除数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendDelData01(String deviceId, Integer data, String serviceType, String miniType,
			String random, String stamp) {
		StringBuilder sendData = new StringBuilder();
		String index = messageCofigBean.getStartWith();
		String outputService = messageCofigBean.getOutputService();
		String end = messageCofigBean.getEndWith();
		sendData.append(index).append(outputService).append(sendType01).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(3, 4)).append(miniType)
				.append(HexUtils.toHex(1, 2)).append(HexUtils.toHex(data, 2)).append(end);
		return sendData.toString();
	}
	public String getSendDelData06(String deviceId, Integer data,String sendType, String serviceType, String miniType,
			String random, String stamp) {
		StringBuilder sendData = new StringBuilder();
		String index = messageCofigBean.getStartWith();
		String outputService = messageCofigBean.getOutputService();
		String end = messageCofigBean.getEndWith();
		sendData.append(index).append(outputService).append(sendType).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(3, 4)).append(miniType)
				.append(HexUtils.toHex(1, 2)).append(HexUtils.toHex(data, 2)).append(end);
		return sendData.toString();
	}
	
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendData(String sendType,String deviceId, Integer dataLength, String data, String serviceType, String miniType,
			String random, String stamp) {
		StringBuilder sendData = new StringBuilder();
		String index = messageCofigBean.getStartWith();
		String outputService = messageCofigBean.getOutputService();
		String end = messageCofigBean.getEndWith();
		Integer maxGroupLength = messageSplitConfigBean.getMaxGroupLength();
		sendData.append(index).append(outputService).append(sendType).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(dataLength +maxGroupLength, 4)).append(miniType)
				.append(HexUtils.toHex(dataLength, 4)).append(data).append(end);
		return sendData.toString();
	}
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendData01(String deviceId, Integer dataLength, String data, String serviceType, String miniType,
			String random, String stamp) {
		StringBuilder sendData = new StringBuilder();
		String index = messageCofigBean.getStartWith();
		String outputService = messageCofigBean.getOutputService();
		String end = messageCofigBean.getEndWith();
		Integer maxGroupLength = messageSplitConfigBean.getMaxGroupLength();
		sendData.append(index).append(outputService).append(sendType01).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(dataLength +maxGroupLength, 4)).append(miniType)
				.append(HexUtils.toHex(dataLength, 4)).append(data).append(end);
		return sendData.toString();
	}
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendEmptyData(String deviceId, String data, String serviceType, String random, String stamp) {
		StringBuilder sendData = new StringBuilder();
		String index = messageCofigBean.getStartWith();
		String outputService = messageCofigBean.getOutputService();
		String end = messageCofigBean.getEndWith();
		sendData.append(index).append(outputService).append(sendType06).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(data).append(end);
		return sendData.toString();
	}

	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendFacesData(String sendType, String deviceId, Integer dataLength, String data, String serviceType,
			String miniType, String random, String stamp,String offsize,Integer count) {
		StringBuilder sendData = new StringBuilder();
		String index = messageCofigBean.getStartWith();
		String outputService = messageCofigBean.getOutputService();
		String end = messageCofigBean.getEndWith();
		Integer offsizeLength = messageSplitConfigBean.getOffsetLength();
		Integer facesLength = messageSplitConfigBean.getFacesLength();
		Integer maxGroupLength = messageSplitConfigBean.getMaxGroupLength();
		sendData.append(index).append(outputService).append(sendType).append(deviceId).append(random)
				.append(serviceType).append(stamp)
				.append(HexUtils.toHex(dataLength +maxGroupLength+offsizeLength+facesLength, 4)).append(miniType)
				.append(HexUtils.toHex(dataLength+offsizeLength+facesLength, 4)).append(HexUtils.toHex(count,8))
				.append(offsize)
				.append(data)
				.append(end);
		return sendData.toString();
	}

	/**
	 * 
	 * @return
	 */
	public String getSendLength(Integer length) {
		return StringUtils.leftPad(length.toString(), 4);
	}

	/**
	 * 
	 * @param imageData
	 * @param index
	 * @return
	 */
	public String spitImageData(byte[] imageData,Integer index,Integer size) {
		if(imageData.length-index<size) {
			size = imageData.length-index;
		}
		byte[] imageHex = Arrays.copyOfRange(imageData, index,  index+size);
		return HexUtils.bytesToHexString(imageHex, size);
	}
	/**
	 * 开锁
	 * 
	 * @param status
	 * @param stamp
	 * @return
	 */
	public String getOpenLockData(String status, String stamp) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(status).append(stamp);
		return sendData.toString();

	}

	/**
	 ** 更换IP跟端口
	 * 
	 * @param status
	 * @param stamp
	 * @return
	 */
	public String getChangeIpPort(String ip, String port) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHexL(ip, 6)).append(HexUtils.toHex(Integer.valueOf(port), 2));
		return sendData.toString();

	}

	/**
	 ** 获取密码数据
	 * 
	 * @param status
	 * @param stamp
	 * @return
	 */
	public String getPassworData(String ip, String port) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHexL(ip, 6)).append(HexUtils.toHex(Integer.valueOf(port), 2));
		return sendData.toString();

	}

	/**
	 * mac 地址字符串
	 * 
	 * @param msg
	 * @return
	 */
	public String getMacString(String msg) {
		return msg.replace(",", "").replace(":", "");
	}
	
	/**
	 * *获取正确返回字符串
	 * 
	 * @param msg
	 * @return
	 */
	public String getMsg(String msg) {
		String start =messageCofigBean.getStartWith()+messageCofigBean.getInputService();
		return msg.substring(msg.lastIndexOf(start), msg.length());
	}
	/**
	 ** 获取下发数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public  String getSendDataInfo(Integer pitNum,String data, String startTime, String endTime) {
		String imageId = StringUtils.leftPad(pitNum.toString(),2, "0");
		String resHex =restring+HexUtils.stringToHexString(imageId);
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2));
		startTime = DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8 + "";
		
		endTime = DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8 + "";
		sendData.append(HexUtils.toHexL(startTime, 8)).append(HexUtils.toHexL(endTime, 8));
		StringBuilder buf = new StringBuilder();
		buf.append(data);
		buf.replace(10, 10+resHex.length(), resHex);
		sendData.append(buf.toString());
		return sendData.toString();
	}
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */
	public   String getSendFacesData(String deviceId, Integer dataLength, String data,String type, String serviceType,
			String random, String stamp) {
		System.out.println("sendData.toString():"+type+":"+serviceType+":"+stamp+":"+random);
		StringBuilder sendData = new StringBuilder();
		String outputService = messageCofigBean.getOutputService();
		String hexDataLength = HexUtils.toHex(dataLength , 4);
		Integer count = (serviceType.length()+hexDataLength.length())/2+dataLength;
		sendData.append(messageCofigBean.getStartWith()).append(outputService).append(sendType08).append(deviceId).append(random)
				.append("0022").append(stamp).append(HexUtils.toHex(count , 4)).append(serviceType).append(hexDataLength)
				.append(data).append(messageCofigBean.getEndWith());
		return sendData.toString();
	}
	/**
	 ** 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendFm223Info(Integer pitNum, String startTime, String endTime, Integer size,String flag) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2));
		startTime = DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8 + "";
		endTime = DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8 + "";
		sendData.append(HexUtils.toHexL(startTime, 8)).append(HexUtils.toHexL(endTime, 8));
		sendData.append(HexUtils.toHex(size/2, 8));
		sendData.append(flag);
		return sendData.toString();
	}
	/**
	 * 获取下发数据
	 * 
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendData0c(String serviceType,String deviceId, Integer dataLength, String data, String groupType, String miniType,
			String random, String stamp) {
		StringBuilder sendData = new StringBuilder();
		String index = messageCofigBean.getStartWith();
		String outputService = messageCofigBean.getOutputService();
		String end = messageCofigBean.getEndWith();
		Integer maxGroupLength = messageSplitConfigBean.getMaxGroupLength();
		sendData.append(index).append(outputService).append(serviceType).append(deviceId).append(random)
				.append(groupType).append(stamp).append(HexUtils.toHex(dataLength + maxGroupLength, 4))
				.append(miniType).append(HexUtils.toHex(dataLength, 4)).append(data).append(end);
		return sendData.toString();
	}
	/**
	 * 
	 * @param imageData
	 * @param index
	 * @return
	 */
	public String spitFm226ImageData(String imageData, Integer index, Integer size) {
		if (imageData.length() - index < size) {
			size = imageData.length() - index;
		}
		String imageHex =imageData.substring(index, index + size);
		return imageHex;
	}
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendFm226FacesData(String deviceId,String sendType, Integer dataLength, String data, String serviceType,
			String miniType, String random, String stamp,  Integer allCount, Integer count) {
		StringBuilder sendData = new StringBuilder();
		String index = messageCofigBean.getStartWith();
		String outputService = messageCofigBean.getOutputService();
		String end = messageCofigBean.getEndWith();
		Integer offsizeLength = messageSplitConfigBean.getOffsetLength();
		Integer facesLength = messageSplitConfigBean.getFacesLength();
		Integer maxGroupLength = messageSplitConfigBean.getMaxGroupLength();
		sendData.append(index).append(outputService).append(sendType).append(deviceId).append(random)
				.append(serviceType).append(stamp)
				.append(HexUtils.toHex((dataLength + maxGroupLength + 8 + facesLength+fm226AllLength)/2, 4)).append(miniType)
				.append(HexUtils.toHex((dataLength + offsizeLength + facesLength+fm226AllLength)/2, 4)).append(HexUtils.toHex(allCount/2, 8))
				.append(HexUtils.toHex(count, 8))
				.append(data).append(end);
		return sendData.toString();
	}
	/**
	 ** 获取OTA下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param packSize包大小
	 * @param version版本，只留数字
	 * @return
	 */

	public String getOTAData(String md5Str, String data, Integer version,Integer packSize) {
		StringBuffer sendData = new StringBuffer();
		String vsSize = HexUtils.toHex(version, 8);
		String pkSize = HexUtils.toHex(packSize/2, 8);
		Integer dataLength = data.length()/2;
		Integer otaLength = md5Str.length()/2+vsSize.length()/2+dataLength;
		sendData.append(OTAFlag).append(otaLength).append(vsSize).append(pkSize).append(dataLength).append(data);
		return sendData.toString();
	}
	@Override
	public void afterPropertiesSet() throws Exception {
		this.index = messageSplitConfigBean.getIndex();
		this.sendType01 = messageCofigBean.getSendType01();
		this.sendType06 = messageCofigBean.getSendType06();
		this.sendType08 = messageCofigBean.getSendType08();
		this.sendTypeLength = messageSplitConfigBean.getSendTypeLength();
		this.deviceTypeLength = messageSplitConfigBean.getDeviceTypeLength();
		this.deviceId = messageSplitConfigBean.getDeviceIdLength();
		this.random = messageSplitConfigBean.getRandomLength();
		this.serviceType = messageSplitConfigBean.getServiceTypeLength();
		this.count = messageSplitConfigBean.getCountLength();
		this.multiple = messageSplitConfigBean.getMultiple();
		this.macLength = messageSplitConfigBean.getMacLength();
		this.offsetLength = messageSplitConfigBean.getOffsetLength();
		this.timestamp=  messageSplitConfigBean.getTimestampLength();
		this.pitNumLength = messageSplitConfigBean.getPitNumLength();
		this.openTypeLength = messageSplitConfigBean.getOpenTypeLength();
		this.sendDataLength = messageSplitConfigBean.getSendDataLength();
	}
	public static void main(String[] args) {
		String msg ="680306d1e60c301000000139d54c040008f106006400023fa90016";
		Integer t1 = 8;
		Integer t2 = 0;
		String start =(t1*60 +t2)/6+"";
		
		System.out.println(HexUtils.toHexL(start, 2));
		
	}
}
